package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "parent_comment_id", property = "parentCommentId"),
        @Result(column = "article_id", property = "articleId"),
        @Result(column = "{parentCommentId=id,articleId=article_id}", property = "replyComments",one = @One(select = "com.example.speeddating.mapper.CommentMapper.findTopCommentsByParentIdAndArticleId")),
        @Result(column = "user_id", property = "userId"),
        @Result(column = "user_id",property = "nickname",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserNickname")),
        @Result(column = "user_id",property = "avator",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvator")),
        @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectCommentLikeNum")),
    })
    @Select(value = "SELECT * FROM `comment` WHERE article_id = #{articleId} AND parent_comment_id = #{parentCommentId} AND deleted = 0 ORDER BY create_time DESC")
    public Page<Comment> findTopCommentsByParentIdAndArticleId(Page<Comment> page, @Param("parentCommentId") Long parentCommentId, @Param("articleId") Long articleId);


    /**
     * 文章评论数
     * @param articleId
     * @return
     */
    @Select(value = "SELECT count(*) FROM `comment` WHERE article_id = #{articleId} AND deleted = 0")
    public Integer selectArticleCommentNum(long articleId);


    /**
     * 是否在今天评论过
     * @param userId
     * @return
     */
    @Select("SELECT count(*) FROM `comment` WHERE DATE(create_time) = CURDATE() AND user_id = #{userId}")
    public int selectIsCommentInToday(long userId);


    /**
     * 查询评论
     * @param id
     * @return
     */
    @Select("SELECT c.* FROM `comment` c,`user` u WHERE c.user_id = u.id  AND c.id = #{id}")
    Comment selectComment(Long id);

    /**
     * 查询回复的人
     * @param parentCommentId
     * @return
     */
    @Select("SELECT u.nickname FROM `comment` c,`user` u WHERE c.id = #{parentCommentId} AND c.user_id = u.id")
    String selectReplyNickname(Long parentCommentId);

    /**
     * 根据文章查询评论 的顶级评论
     * @param articleId
     * @return
     */
    @Select("select * from `comment` where article_id = #{articleId} and deleted = 0 AND parent_comment_id =-1 ORDER BY create_time DESC")
    List<Comment> getCommentsByArticleId(Long articleId);

    /**
     * 根据评论查询回复评论的
     */
    @Select("SELECT * FROM `comment` WHERE parent_comment_id in (SELECT id FROM `comment` WHERE user_id = #{userId}) AND article_id =#{articleId} ORDER BY create_time DESC")
    List<Comment> getReplayComment(Long userId,Long articleId);


    /**
     * 评论文章未读的
     * @param userId
     * @return
     */
    @Select("select count(*) from `comment` where article_id in (select id from article where user_id = #{userId} and deleted = 0) AND user_id !=#{userId} and deleted = 0 AND is_read = 0")
    public Integer getUnreadCountCommentByArticleId(Long userId);

    /**
     * 回复评论未读的
     * @param userId
     * @return
     */
    @Select("SELECT count(*) FROM `comment` WHERE parent_comment_id in (SELECT id FROM `comment` WHERE user_id = #{userId} and deleted = 0)  AND user_id !=#{userId} AND  deleted = 0 AND is_read = 0")
    public Integer getUnreadCommentInReplayComment(Long userId);


    /**
     * update评论文章未读的
     * @param userId
     * @return
     */
    @Update("update `comment` set is_read = 1 where article_id in (select id from article where user_id = #{userId} and deleted = 0) and deleted = 0 AND is_read = 0 AND user_id !=10015")
    public Integer updateUnreadCountCommentByArticleId(Long userId);

    /**
     * update回复评论未读的
     * @param userId
     * @return
     */
    @Update("UPDATE `comment` AS c SET c.is_read = 1 WHERE c.parent_comment_id IN \n" +
            "(SELECT id FROM ( SELECT id FROM `comment` WHERE user_id = #{userId} AND deleted =0) AS subquery)AND c.deleted = 0 AND c.is_read = 0 AND c.user_id != 10015")
    public Integer updateUnreadCommentInReplayComment(Long userId);





}
